
package org.argeproje.resim.proc.math;
import org.argeproje.resim.proc.ConnectionRule;
import org.argeproje.resim.proc.Processor;
import org.argeproje.resim.proc.data.ConstDA;
import org.argeproje.resim.proc.data.Data;
import org.argeproje.resim.proc.data.ImageDA;


public class LogicalPR extends MathPR {
	protected int _opType;
	protected void setConnectionRules(ConnectionRule connRule){
		connRule.addInType(ImageDA.class, 0);
		connRule.addInType(ConstDA.class, 0);
		connRule.addInType(ImageDA.class, 1);
		connRule.addInType(ConstDA.class, 1);		
		connRule.addOutType(ImageDA.class);
	}
	public void setParameters(int opType) {
		setParamOpType(opType);
	}

	public void setParamOpType(int opType) {
		_opType = opType;
	}

	public int getParamOpType() {
		return _opType;
	}

	public Data process() {
	   	Processor pr = getProcessor(getInput());
	    setOutput(pr.process());
	    return getOutput();
	}
	  private Processor getProcessor(Data input) {
		boolean constOp = false;
		boolean imageOp = false;
		Data input0 = getInput(0);
		Data input1 = getInput(1);
		if ( (input0.isOfType(Data.CONST) && input1.isOfType(Data.IMAGE))
				|| (input0.isOfType(Data.IMAGE) && input1.isOfType(Data.CONST)) ) {
			constOp = true;
		}
		if (input0.isOfType(Data.IMAGE) && input1.isOfType(Data.IMAGE)){
			imageOp = true;
		}
		if(constOp){
			ConstLogicalPR pr = new ConstLogicalPR();
			pr.setParameters(getParamOpType());
			ConstDA constDA = null;
			ImageDA img = null;
			if(input.isOfType(Data.CONST) ){
				constDA = (ConstDA)input;
				img = (ImageDA)input1;
			}
			else{
				constDA = (ConstDA)input1;
				img = (ImageDA)input0;
			}
		    Data[] inputs = new Data[2];
		    inputs[0]= img;
		    inputs[1] = constDA;
		    pr.setInputs(inputs);
		    return pr;
		}
		else if(imageOp){
			ImageLogicalPR pr = new ImageLogicalPR();
			pr.setParameters(getParamOpType());
			pr.setInputs(getInputs());
			return pr;
		}
		return null;
	}
}
